package com.gxa.hualian.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.hualian.mapper.TicketMapper;
import com.gxa.hualian.pojo.Ticket;
import com.gxa.hualian.service.TicketService;
import com.gxa.hualian.util.JsonResult;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * @Author 杨天可
 * @Date 2020/10/20 9:51
 * @Version 1.0
 */
@RestController
@RequestMapping("/tkt")
@Transactional
public class TicketController {
    @Resource
    TicketService ticketService;
    @Resource
    TicketMapper ticketMapper;

    //新增优惠券
    @PostMapping(value = "/add")
    public int add(@RequestBody Ticket ticket){
        int add = ticketService.add(ticket);
        return add;
    }

    //根据主键查找优惠券
    @PostMapping(value = "/getTktById")
    public Map getTktById(@RequestParam  Integer id){
        Ticket tktById = ticketService.getTktById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(tktById);
        return jsonResult.getValues();
    }

    //优惠券查询分页列表
    @PostMapping(value = "/getTktList")
    public Map getTktList(@RequestParam("page") Integer page,@RequestParam("limit") Integer limit){
        PageHelper.startPage(page,limit);
        List<Ticket> tktList = ticketService.getTktList();
        PageInfo<Ticket> pageInfo = new PageInfo<>(tktList);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(pageInfo);
        return jsonResult.getValues();
    }

    //导出优惠券列表
    @RequestMapping("/exportTktList")
    public void handle(HttpServletResponse response) throws IOException {


        // 先获取数据
        List<Ticket> tickets =  ticketMapper.selectAll();

        // POI 实现
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("ticket");  // 工作表的名称
        String fileName = "优惠券列表.xls";   // 文件名
        // 完美实现中文文件名的转换
        fileName = URLEncoder.encode(fileName,"utf-8").replaceAll("\\+", "%20");
        // headers表示excel表中第一行的表头 在excel表中添加表头
        String[] headers = { "编号", "名称", "描述","使用规则","领取时间","使用时间","领取状态","地址"};
        HSSFRow row = sheet.createRow(0);
        for(int i=0;i<headers.length;i++){
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //准备将时间格式处理为标准日期格式
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 添加数据行:
        int rowNum = 1;
        for (Ticket item : tickets) {
            HSSFRow dataRow = sheet.createRow(rowNum);
            dataRow.createCell(0).setCellValue(rowNum);   // 自增长变量
            dataRow.createCell(1).setCellValue(item.getName());
            dataRow.createCell(2).setCellValue(item.getDesc());
            dataRow.createCell(3).setCellValue(item.getRole());
            dataRow.createCell(4).setCellValue(dateFormat.format(item.getCollectExpired()));
            dataRow.createCell(5).setCellValue(dateFormat.format(item.getUseExpired()));
            dataRow.createCell(6).setCellValue(item.getState());
            dataRow.createCell(7).setCellValue(item.getImg());
            rowNum++;
        }

        //返回给前端下载
        response.setContentType("application/octet-stream");
        // 以附件形式访问
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        response.flushBuffer();
        workbook.write(response.getOutputStream());
    }

    //修改优惠券状态
    @PostMapping("/updateState")
    public Map updateState(@RequestParam("id") Integer id,@RequestParam("state") Integer state){

        return ticketService.updateState(id,state);
    }
}
